import { oss_signature } from '@/services/getOssData';
import { random_string } from './randomstring';
// 阿里云 后端服务器签名上传

/**
 * 获取后端签名
 * @param {*} info 文件信息
 */
var glo_image_data: any;
export const get_acceass = async (info: any) => {
  let result = await oss_signature();
  let res = result.data;
  console.log('zjj', result);
  let expiretime = res.expire * 1000;
  let nowtime = Date.parse(String(new Date()));

  //上传路径
  let video_path = '';
  console.log(res);
  if (process.env.UMI_ENV === 'test') {
    video_path = 'pinggu_test/';
  }
  if (process.env.UMI_ENV === 'dev') {
    video_path = 'dev/';
  }
  if (process.env.UMI_ENV === 'prod') {
    video_path = 'pinggu_prod/';
  }
  var str = info.name ? info.name : info.file.name;
  str = str.substring(str.lastIndexOf('.'));
  video_path += random_string(10) + str;
  let filePath = res.host + '/' + video_path;
  if (expiretime > nowtime) {
    glo_image_data = {
      OSSAccessKeyId: res.accessid, //需要根据自己的bucket填写 详情请见oss api
      policy: res.policy, //policyBase64规定了请求的表单域的合法性
      signature: res.signature, //根据Access Key Secret和policy计算的签名信息，OSS验证该签名信息从而验证该Post请求的合法性
      success_action_status: '200', // 让服务端返回200,不然，默认会返回204
      key: video_path, //文件名字，可设置路径
      file: info.name ? info : info.file,
    };
  }
  return { ossHost: res.host, glo_image_data, filePath };
};
